<?php 

class TesisWizard
{

	private $current_step = 0;
	private $steps_count = 0;
	private $form = null;
	private $sel_option = '';
	private $schema = array();

	public function __construct()
	{
		$this->steps_count = 3;
		
		$this->schema = array(
			0 => array(
				'model' => '',
				'label' => 'Elija una opción'
			),
			1 => array(
				'model' => 'Pertinencia',
				'label' => 'Datos de la pertinencia',
				'relations' => array('model' => 'Estudiante', 'key' => 'estudiante_list')
			),
			2 => array(
				'model' => 'Tesis',
				'label' => 'Datos del proyecto'
			)
		);
	}

	public function setStep($step)
	{
		$this->current_step = $step;
	}

	public function getForm()
	{
		if ($this->form == null)
			$this->updateForm();

		return $this->form;
	}

	private function updateForm($record = null)
	{
		switch($this->current_step)
		{
			case 0:
				$this->form = new OpcionesForm();
				break;
			case 1:
				if ($this->sel_option == OpcionesForm::$OPC_SEL)
					$this->form = new BuscadorPertinenciaForm();
				else
				{
					$this->form = new PertinenciaForm($record);
					unset($this->form['aprobado']);
				}
				break;
			case 2:
				$this->form = new TesisForm($record);
				unset($this->form['pertinencia_id']);
		}
	}

	private function recordFromAttr($attr, $step)
	{
		$values = $this->getAttribute($attr);

		if ($values != null)
		{
			$record = new $attr();
			$record->fromArray($values);

			if (isset($this->schema[$step]['relations']))
			{
				$relation = $this->schema[$step]['relations'];
				if (isset($values[$relation['key']]))
					$record->link($relation['model'], $values[$relation['key']]);
			}

			return $record;
		}

		return null;
	}

	public function nextStep()
	{
		$this->current_step = $this->current_step + 1;
		$this->updateForm($this->recordFromAttr($this->getModel(), $this->current_step));
	}

	public function previousStep()
	{
		$this->current_step = $this->current_step - 1;
		$this->updateForm($this->recordFromAttr($this->getModel(), $this->current_step));
	}

	private function getAttribute($attr)
	{
		return sfContext::getInstance()->getUser()->getAttribute($attr, null);
	}

	private function setAttribute($attr, $value)
	{
		sfContext::getInstance()->getUser()->setAttribute($attr, $value);
	}

	private function removeAttributes()
	{
		$holder = sfContext::getInstance()->getUser()->getAttributeHolder();

		foreach ($this->schema as $value)
			$holder->remove($value['model']);
	}

	private function getModel()
	{
		return $this->schema[$this->current_step]['model'];
	}

	public function clean()
	{
		$this->removeAttributes();
	}

	public function bind(sfWebRequest $request)
	{
		if ($this->current_step == 0)
		{
			$values = $request->getParameter('opciones');
			$this->setAttribute(OpcionesForm::$KEY, $values[OpcionesForm::$KEY]);
		}

		$this->sel_option = $this->getAttribute(OpcionesForm::$KEY);
		$this->form = $this->getForm();
		$this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
		
		$values = $this->form->getValues();

		switch ($this->current_step)
		{
			case 1:
				$this->setAttribute($this->getModel(), $values);
				break;
			case 2:
				$this->setAttribute($this->getModel(), $values);
		}
	}

	public function save()
	{
		$tesis = new Tesis();
		$tesis->fromArray($this->getAttribute('Tesis'));
		$tesis->setEstado(Constantes::ACTIVO);

		$values = $this->getAttribute('Pertinencia');

		if ($this->sel_option == OpcionesForm::$OPC_AGR)
		{
			$pertinencia = $this->recordFromAttr('Pertinencia', 1);
			$pertinencia->setAprobado(true);
			$tesis->setPertinencia($pertinencia);
		}
		else
		{
			$pertinencia_id = $values['id'];
			$tesis->setPertinenciaId($pertinencia_id);
		}

		$form = new TesisForm($tesis);
		$con = $form->getConnection();

		try
		{
			$con->beginTransaction();
			$tesis->save($con);
			$con->commit();
			$this->removeAttributes();
		}
		catch (Exception $e)
		{
			$con->rollBack();
			throw $e;
		}

		return $tesis;
	}

	public function getSteps()
	{
		$steps = array();
		foreach ($this->schema as $value)
			$steps[] = $value['label'];

		return $steps;
	}

	public function getCurrentStep()
	{
		return $this->current_step;
	}
	
	public function getStepsCount()
	{
		return $this->steps_count;
	}
	
	public function isFirstStep()
	{
		return $this->current_step == 0;
	}
	
	public function isLastStep()
	{
		return $this->current_step == $this->steps_count - 1;
	}

}
